from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, func
from sqlalchemy.orm import relationship

from app.bot.services.date_time import get_date_time
from app.database.models.base import Base
from app.database.models.m2ms import allowed_ips_orders


class AllowedIP(Base):
    __tablename__ = "allowed_ip"

    id = Column(Integer, primary_key=True)
    ip_address = Column(String(255), nullable=False, unique=True)
    user = relationship("User", back_populates="allowed_ips")
    user_id = Column(Integer, ForeignKey("user.id"))
    orders = relationship(
        "Order", secondary=allowed_ips_orders, back_populates="allowed_ips"
    )
    created_at = Column(DateTime, server_default=func.now())

    def __repr__(self):
        return f"<Allowed IP №{self.id} {self.ip_address}>"

    def __str__(self):
        return self.__repr__()
